import React from 'react'
import styles from '../index.module.scss'
import { LockOutlined, UserOutlined } from '@ant-design/icons'
import { Button, Form, Input, Select, message } from 'antd'
import { useForm } from 'antd/es/form/Form'
import { addAdminApi } from '@/common/api'
export default function AddAdmin(props) {
  const [form] = Form.useForm()
  const AddAdmin = () => {
    form.validateFields().then(async(values) => {
      const RES = await addAdminApi(values)
      if (RES.code == 200) {
        message.success(RES.msg)
        props.setAdminDataSource(values)
      }
    }).catch((err) => {
      console.log(err);
    })
  }
  return (
    <div className={styles.addAdmin}>
      <Form form={form}>
        <Form.Item
          name="username"
          rules={[{ required: true, message: '请填写账号' }]}
        >
          <Input prefix={<UserOutlined />} placeholder="账号" />
        </Form.Item>
        <Form.Item
          name="password"
          rules={[{ required: true, message: '请填写密码' }]}
        >
        <Input prefix={<LockOutlined />} type="password" placeholder="密码" />
        </Form.Item>
        <Form.Item
          name="newPassword"
          rules={
            [{
            required: true,
            message: "密码不一致",
            validator: (_, value) => {
                if (value.trim()!==form.getFieldValue('password')) {
                    return Promise.reject()
                }
                return Promise.resolve()
            }
            }]
          }
        >
        <Input prefix={<LockOutlined />} placeholder="确认密码" />
        </Form.Item>
        <Form.Item
          name="name"
          rules={[{ required: true, message: '请输入姓名' }]}
        >
        <Input  placeholder="姓名" />
        </Form.Item>
        <Form.Item
          name="role"
        initialValue="0"
        >
          <Select placeholder="请选择角色">
            <Select.Option value="0">普通管理员</Select.Option>
            <Select.Option value="1">超级管理员</Select.Option>
          </Select>
        </Form.Item>
        <Button type='primary' onClick={AddAdmin}>
          添加
        </Button>
      </Form>
    </div>
  )
}
